<?php

namespace backend\models;

/**
 * @author  shuang
 * @date    2016-11-16 8:32:17
 * @version V1.0
 * @desc    
 */
use common\models\Task;
use Yii;

class MY_Task extends Task {

    public static function getList() {
        $query = MY_Task::find();
        $query->leftJoin(["user" => "me_user"], "me_task.userid=user.id");
        $query->leftJoin(["price" => MY_TaskPrice::tableName()], "price.id=me_task.task_price_id");
        $query->leftJoin(["tasktype" => MY_Dictionary::tableName()], "tasktype.id=me_task.type");
        $query->leftJoin(["taskstatus" => MY_Dictionary::tableName()], "taskstatus.id=me_task.status");
        $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as stacount,taskid FROM " . MY_UserTaskOperate::tableName() . "  WHERE status= 1 GROUP BY taskid) as b", "me_task.id=b.taskid");
        $query->leftJoin("(SELECT COUNT(`me_task_hidden`.`id`) as hidcount,taskid FROM " . \frontendm\models\WB_TaskHidden::tableName() . "  WHERE hidden_time= 1 GROUP BY taskid) as c", "me_task.id=c.taskid");
        $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as comcount,taskid FROM " . MY_UserTaskOperate::tableName() . "  WHERE is_complain= 2 GROUP BY taskid) as d", "me_task.id=d.taskid");

        $query->select("me_task.*,user.username,b.stacount,c.hidcount,d.comcount,tasktype.name as typename,taskstatus.name as statusname");
        $query->andFilterWhere(["like", "me_task.title", Yii::$app->request->get("title")]);
        $query->andFilterWhere(["like", "user.username", Yii::$app->request->get("username")]);
        $query->andFilterWhere(["like", "me_task.id", Yii::$app->request->get("taskid")]);
        if (Yii::$app->request->get("status") > 0) {
            $query->andWhere("me_task.status=:status", [":status" => Yii::$app->request->get("status")]);
        }
        if (Yii::$app->request->get("type") > 0) {
            $query->andWhere("me_task.type=:type", [":type" => Yii::$app->request->get("type")]);
        }
        $countQuery = clone $query;
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $totalCount = $countQuery->count();
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["total" => $totalCount, "data" => $res];
    }

    /*
     * 关联字典表  任务状态
     */

    public function getStatusID() {

        return $this->hasOne(WB_Dictionary::className(), ['id' => 'status']);
    }

    /*
     * 关联单价
     */

    public function getPriceID() {
        return $this->hasOne(WB_TaskPrice::className(), ['id' => 'task_price_id']);
    }

    /*
     * 获取任务详情
     */

    public static function getTaskDetail($id) {
        $query = MY_Task::find();
        $query->leftJoin(["user" => "me_user"], "me_task.userid=user.id");
        $query->leftJoin(["price" => MY_TaskPrice::tableName()], "price.id=me_task.task_price_id");
        $query->leftJoin(["tasktype" => MY_Dictionary::tableName()], "tasktype.id=me_task.type");
        $query->leftJoin(["taskstatus" => MY_Dictionary::tableName()], "taskstatus.id=me_task.status");
        $query->select("me_task.*,user.username,price.price,price.brush_price,tasktype.name as typename,taskstatus.name as statusname");
        $query->where("me_task.id=:id", [":id" => $id]);
        return $query->asArray()->one();
    }

}
